import router from './router'
import store from './store'
import { Message } from 'element-ui'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import { getToken } from '@/utils/auth' // get token from cookie
import getPageTitle from '@/utils/get-page-title'

NProgress.configure({ showSpinner: false }) // NProgress Configuration

const whiteList = ['/login'] // 白名单

// 全局前置导航守卫
router.beforeEach(async (to, from, next) => {
    // 开启进度条
    NProgress.start()
    const token = store.getters.token
    // const token = getToken()
    if (token) {
        // 是否去登陆页面
        if (to.path === '/login') {
            next('/')
        } else {
            if (!store.getters.userId) {
                await store.dispatch('user/getUserProfileAction')
            }
            next()
        }
    } else {
        if (whiteList.includes(to.path)) {
            next()
        } else {
            next('/login')
        }
    }
    // 设置标题
    document.title = getPageTitle()
    // 关闭进度条
    NProgress.done()
})

// 全局后置导航守卫
router.afterEach(() => {
    // 关闭进度条
    NProgress.done()
})
